<?php
class GroupsController extends AppController {

    public $helpers = array('Pagination');

    public function admin_index() {
        $this->Group->recursive = 1;
        $this->set('groups', $this->paginate());
    }

    public function admin_add() {
        if (!empty($this->data)) {
            $this->Group->create();
            if ($this->Group->save($this->data)) {
                $this->Session->setFlash(__('The Group has been saved',true));
                $this->redirect(array('action'=>'index'), null, true);
            } else {
                $this->Session->setFlash(__('The Group could not be saved. Please, try again.',true));
            }
        }
    }

    public function admin_edit($id = null) {
        if (!$this->Group->findById($id)) {
            $this->Session->setFlash(__('Invalid group',true));
            $this->redirect(array('action'=>'index'), null, true);
        }
        if (!empty($this->data)) {
            if ($this->Group->save($this->data)) {
                $this->Session->setFlash(__('The Group has been saved',true));
                $this->redirect(array('action'=>'index'), null, true);
            } else {
                $this->Session->setFlash(__('The Group could not be saved. Please, try again.',true));
            }
        } else {
            $this->data = $this->Group->read(null, $id);
        }
    }

    public function admin_delete($id = null) {
        if (!$this->Group->findById($id)) {
            $this->Session->setFlash(__('Invalid group',true));
        }
        if ($this->Group->del($id)) {
            $this->Session->setFlash(__('Group deleted',true));
        } else {
            $this->Session->setFlash(__('The Group could not be deleted. Please, try again.',true));
        }
        $this->redirect(array('action'=>'index'), null, true);
    }

    public function admin_delete_groups() {
        if($this->Group->deleteAll(array('Group.id'=>$this->data['GroupsDelete']))) {
            $this->Session->setFlash(__('Groups deleted',true));
        } else {
            $this->Session->setFlash(__('The Groups could not be deleted. Please, try again.',true));
        }
        $this->redirect(array('action'=>'index'), null, true);
    }
}
?>